﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyStep.TestExecution.Interfaces;
using EasyStep.TestExecution.Data;
using EasyStep.Business.Interfaces;
using System.Drawing;

namespace EasyStep.TestExecution.Browsers
{
    public abstract class BaseWebBrowserProxy<T>:IWebBrowserProxy
        where T : class
    {
        public BaseWebBrowserProxy(string name, string icon, string description)
        {
            Name = string.IsNullOrEmpty(name) ? this.GetType().Name : name;
            Icon = string.IsNullOrEmpty(icon) ? string.Concat(name, ".png") : icon;
            Description = description;
        }

        public virtual void ExecuteStep(IExecuteDataContext context, TestStepResult data) 
        {
            var actionType = data.StepInfo.Action.CurrentType;
            data.ExecutionLog.Append("execute test step action:");
            if (data.StepInfo.Action.Parent != null)
                data.ExecutionLog.Append(data.StepInfo.Action.Parent.Name).Append(", ");
            data.ExecutionLog.Append(data.StepInfo.Action.Name).AppendLine();

            ITestAction<T> action = null;
            try
            {
                var manager = context.BrowserInstance as T;
                action = Activator.CreateInstance(data.StepInfo.Action.CurrentType, new object[] { }) as ITestAction<T>;
                action.Execute(manager, data, context);
            }
            catch (Exception ex)
            {
                var error = string.Format("Can't execute action:{0}, error!\r\n{1}" , data.StepInfo.Action.Name , ex.Message);
                data.Result = TestExecution.ExecutionResult.Failed;
                data.ExecutionLog.AppendLine(error);
                context.Log.Error(error, ex);
            }
        }
        
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Description { get; set; }

        public abstract object GetNewInstance(IExecuteDataContext context);
        public abstract void FinalizeInstance(object instanceData);
        public abstract Image MakeScreenshot(IExecuteDataContext DataContext, TestStepResult data);


        
    }
}
